package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ExportDao;
import cn.itcast.dao.cargo.PackingDao;
import cn.itcast.dao.cargo.ShippigOrderDao;
import cn.itcast.domain.cargo.Export;
import cn.itcast.domain.cargo.Packing;
import cn.itcast.domain.cargo.ShippigOrder;
import cn.itcast.domain.cargo.ShippigOrderExample;
import cn.itcast.service.cargo.ShippingOrderService;
import cn.itcast.vo.ShippingOrderVo;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class ShippingOrderServiceImpl implements ShippingOrderService {

    /*
    * 注入Dao
    * */
    @Autowired
    private ShippigOrderDao shippigOrderDao;

    @Autowired
    private PackingDao packingDao;

    @Autowired
    private ExportDao exportDao;

    /*
    * 删除
    * */
    @Override
    public void delete(String id) {
        ShippigOrder shippigOrder = shippigOrderDao.selectByPrimaryKey(id);
        Integer state = shippigOrder.getState();
        if(state == 0){
            shippigOrderDao.deleteByPrimaryKey(id);
        }
    }

    @Override
    public void update(ShippigOrder shippingOrder) {

    }

    /*
    * 根据id查询
    * */
    @Override
    public ShippigOrder findById(String id) {
        return shippigOrderDao.selectByPrimaryKey(id);
    }


    /*
    * 查询ShippingOrderVo
    * */
    @Override
    public ShippingOrderVo findVoById(String id) {
        return shippigOrderDao.findVoById(id);
    }

    //查询所有委托信息
    public List<ShippigOrder> findAll(ShippigOrderExample example){
        return shippigOrderDao.selectByExample(example);
    }

    /*
    * 多条件分页查询
    * */
    @Override
    public PageInfo findAll(int page, int size, ShippigOrderExample example) {
        PageHelper.startPage(page,size);
        List<ShippigOrder> list = shippigOrderDao.selectByExample(example);
        return new PageInfo(list);
    }

    /*
    * 保存
    * */
    @Override
    public void save(ShippigOrder shippigOrder) {
        Packing packing = packingDao.selectByPrimaryKey(shippigOrder.getId());
        String[] ids = packing.getExportIds().split(",");
        Export export = exportDao.selectByPrimaryKey(ids[0]);
        shippigOrder.setLcNo(export.getLcno());
        shippigOrder.setPortOfLoading(export.getShipmentPort());
        shippigOrder.setPortOfDischarge(export.getDestinationPort());
        shippigOrder.setState(0);
        shippigOrderDao.insertSelective(shippigOrder);
        //修改装箱单状态
        packing.setState(1);
        packingDao.updateByPrimaryKeySelective(packing);
    }

}
